import 'package:word_range_client/config/RepositoryConfig.dart';
import "package:word_range_client/pojo/Result.dart";
import 'package:word_range_client/pojo/queery/WordQuery.dart';
import 'package:word_range_client/repositories/HttpService.dart';

import '../pojo/dto/PageDto.dart';

class WordBookRepository{
  static String get _serverUrl => RepositoryConfig().serverUrl;
  static final HttpService _httpService = HttpService(_serverUrl);

  static Future<Result> getAllBookBaseInfo() async {
    try{
      Result result = await _httpService.get("apiV1/wordBook/allBookInfo");
      return result;
    }catch(e){
      throw Exception("获取词书列表失败：$e");
    }
  }

  static Future<Result> getBookInfo(String id) async {
    try{
      Result result = await _httpService.get("apiV1/wordBook/$id");
      return result;
    }catch(e){
      throw Exception("获取词书详情失败：$e");
    }
  }

  static Future<PageDto> getWordListOnBookId(WordQuery wordQuery) async {
    try{
      PageDto result = await _httpService.getPageDto("apiV1/wordBook/allWord", queryParameters: wordQuery.toJson());
      return result;
    }catch(e){
      throw Exception("获取单词列表详情失败：$e");
    }
  }

  // 获取每日一词
  static Future<Result> getRandomWordByDay() async {
    try{
      Result result = await _httpService.get("apiV1/wordMapping/getRandomWordByDay");
      return result;
    }catch(e){
      throw Exception("获取每日一词失败：$e");
    }
  }

  // 存储学习数据
  static Future<Result> saveOrUpdateReview(String word) async {
    try{
      Result result = await _httpService.post("apiV1/review/saveOrUpdateReview", {"word":word});
      return result;
    }catch(e){
      throw Exception("保存学习数据失败：$e");
    }
  }

  // 获取随机新单词列表
  static Future<Result> getRandomNewWordList(String bookId,int count) async {
    try{
      Result result = await _httpService.get(
          "apiV1/review/getRandomNewWordList",
          queryParameters: {"bookId":bookId,"count":count.toString()});
      // 确保返回的 data 是一个 List<Map<String, dynamic>>
      if (result.data is List) {
        return result;
      } else {
        throw Exception("返回的数据格式不正确");
      }
    }catch(e){
      throw Exception("获取随机新单词列表失败：$e");
    }
  }

  // 获取随机复习单词列表
  static Future<Result> getRandomReviewWordList(String bookId,int count) async {
    try{
      Result result = await _httpService.get("apiV1/review/getRandomReviewWordList", queryParameters: {"bookId":bookId,"count":count});
      return result;
    }catch(e){
      throw Exception("获取随机复习单词列表失败：$e");
    }
  }

  // 复习活动获取单词列表的接口
  static Future<Result> getRandomReviewWordListByEvent(int count) async {
    try{
      Result result = await _httpService.get("apiV1/review/getRandomReviewWordListByEvent", queryParameters: {"count":count});
      return result;
    }catch(e){
      throw Exception("获取复习活动单词列表失败：$e");
    }
  }

  // 获取已过招单词数量
  static Future<Result> getReviewedWordCount() async {
    try{
      Result result = await _httpService.get("apiV1/review/getReviewedWordCount");
      return result;
    }catch(e){
      throw Exception("获取已过招单词数量失败：$e");
    }
  }

  // 获取已掌握单词数量
  static Future<Result> getMasteredWordCount() async {
    try{
      Result result = await _httpService.get("apiV1/review/getMasteredWordCount");
      return result;
    }catch(e){
      throw Exception("获取已掌握单词数量失败：$e");
    }
  }

}